package com.gurid.service.vod.service.impl;

import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.exceptions.ClientException;
import com.gurid.common.base.result.ResultCode;
import com.gurid.service.base.exception.GuridException;
import com.gurid.service.vod.service.VideoService;
import com.gurid.service.vod.util.AliyunSdkUtils;
import com.gurid.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VodProperties vodProperties;

    @Override
    public String uploadVideo(InputStream inputStream, String originalFilename) {

        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        System.out.println("--------------title-----" + title);
        System.out.println("---------------filename-----" + originalFilename);
        UploadStreamResponse response = AliyunSdkUtils.uploadVideoByInputStream(
                vodProperties.getKeyid(),
                vodProperties.getKeysecret(),
                title,
                originalFilename,
                inputStream);

        String videoId = response.getVideoId();

        if (!StringUtils.hasText(videoId)) {
            log.error("阿里云上传失败：" + response.getCode() + "~~" + response.getMessage());
            throw new GuridException(ResultCode.VIDEO_UPLOAD_ALIYUN_ERROR);
        }

        return videoId;
    }

    @Override
    public void removeVideo(String videoId) throws ClientException {
        AliyunSdkUtils.deletVideoById(
                vodProperties.getKeyid(),
                vodProperties.getKeysecret(),
                videoId);
    }

    @Override
    public void removeVideoByIdList(List<String> videoIdList) throws ClientException {
        //每个id的长度为 “3045695e1c0b71efbf955017f0e80102” 32 位
        int size = videoIdList.size();//id列表的长度
        //String定义的长度限制为65534，实际应用中是够用的，但StringBuffer长度取决于内存大小
        //String idListStr = String.join(",", videoIdList);
        StringBuffer idListStr = new StringBuffer();
        for (int i = 0; i < size; i++) {
            idListStr.append(videoIdList.get(i));
            if ((i == size - 1) || (i % 20 == 19)) {
                //log.info("idListStr:{}", idListStr);
                //执行删除操作
                AliyunSdkUtils.deletVideoById(
                        vodProperties.getKeyid(),
                        vodProperties.getKeysecret(),
                        idListStr.toString()
                );
                //重置idListStr
                idListStr = new StringBuffer();
            } else {
                idListStr.append(",");
            }
        }

    }

    @Override
    public String getPlayAuth(String videoSourceId) throws ClientException {
        return AliyunSdkUtils.getPlayAuthByVideoId(
                vodProperties.getKeyid(),
                vodProperties.getKeysecret(),
                videoSourceId);
    }


}
